package com.shigu.wxpaysdk.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Properties;


/**
 * Created by wxc on 2016/12/13.
 *
 * @author wxc
 * @version wxpaysdk 0.0.1
 * @since wxpaysdk 0.0.1
 */
public class Configure {

    private static final Logger log = LoggerFactory.getLogger(Configure.class);
    public static String appid;
    public static String mch_id;
    public static String key;
    private static String p12;
    public static boolean test;

    static KeyStore truststore;

    static {
        Properties properties = new Properties();
        try {
            properties.load(Configure.class.getResourceAsStream("/wxpay.properties"));
            appid = properties.get("appid").toString();
            mch_id = properties.get("mch_id").toString();
            key = properties.get("key").toString();
            p12 = properties.get("p12").toString();
            test = Boolean.parseBoolean(properties.get("test").toString());
        } catch (Exception e) {
            log.error("请在classpath根目录正确创建wxpay.properties配置文件，正确配置appid、mch_id、key、p12(API秘钥路径)\n" +
                    "可选参数test=true/false,用以指定SDK访问微信支付仿真系统或正式服", e);
        }
        if (p12 != null && mch_id != null)
            try {
                truststore = KeyStore.getInstance("PKCS12");
                truststore.load(new FileInputStream(p12), mch_id.toCharArray());
            } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException e) {
                e.printStackTrace();
            }

    }

}
